package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.config.ValidationResult;
import com.chinairi.mall.config.ValidatorImpl;
import com.chinairi.mall.controller.viewobject.TCVO;
import com.chinairi.mall.controller.viewobject.TInforCVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.TrackConfig;
import com.chinairi.mall.model.TrackInforConfig;
import com.chinairi.mall.dao.TrackInforConfigMapper;
import com.chinairi.mall.service.ITrackInforConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 节点通知模板配置表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-08-17
 */
@Service
public class TrackInforConfigServiceImpl extends ServiceImpl<TrackInforConfigMapper, TrackInforConfig> implements ITrackInforConfigService {
    @Autowired
    private TrackInforConfigMapper trackInforConfigMapper;
    @Autowired
    private ValidatorImpl validator;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addPoint(TInforCVO tInforCVO) {

        ValidationResult result = validator.validate(tInforCVO);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tInforCVO.getTrackNumber(), tInforCVO.getTrackInformation(), tInforCVO.getTinfcId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }
        TrackInforConfig trackInforConfig = new TrackInforConfig();
        BeanUtils.copyProperties(tInforCVO,trackInforConfig);
        trackInforConfig.setTinfcId(UUID.randomUUID().toString().replace("-",""));
        trackInforConfigMapper.insert(trackInforConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updatePoint(TInforCVO tInforCVO) {

        ValidationResult result = validator.validate(tInforCVO);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tInforCVO.getTrackNumber(), tInforCVO.getTrackInformation(), tInforCVO.getTinfcId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackInforConfig trackInforConfig = new TrackInforConfig();
        BeanUtils.copyProperties(tInforCVO,trackInforConfig);
        trackInforConfigMapper.updateById(trackInforConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deletePoint(String uuid) {
        trackInforConfigMapper.deleteById(uuid);
    }

    @Override
    @DoPage
    public List<TrackInforConfig> getPoints(TInforCVO tInforCVO) {
        QueryWrapper<TrackInforConfig> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotEmpty(tInforCVO.getTrackNumber()),"TRACK_NUMBER",tInforCVO.getTrackNumber());
        List<TrackInforConfig> list = trackInforConfigMapper.selectList(wrapper);
        return list;
    }

    public boolean checkAllExist(String number, String infor, String id) {
        return trackInforConfigMapper.countByAll(number,infor,id) > 0;
    }
}
